package jgl.vajra.management.widget.manager;

import android.content.Intent;
import androidx.fragment.app.FragmentActivity;

import jgl.vajra.management.widget.bean.UpdateApkBean;
import jgl.vajra.management.widget.bean.UpdateApkInfo;
import jgl.vajra.management.widget.dialogfragment.UpdateVersionDialogFragment;
import jgl.vajra.management.widget.mvvm.view.AppActivityManager;
import jgl.vajra.management.widget.utils.FileUtils;

import java.io.File;
import java.io.IOException;

/**
 * Created by Administrator on 2018/9/18.
 */

public class UpdateApkManager {


    private long exitTime;

    public File getApkFile(String netVersion) {
        File apkFolderFile = FileUtils.getApkFolderFile();
        return new File(apkFolderFile, getFileName(netVersion));

    }


    public String getFileName(String netVersion) {
        return "prefee" + netVersion + ".apk";
    }

    public File getApkFilePath(String netVersion) {
        String path = "prefee" + netVersion + ".apk";
        File file = new File(path);
        if (!file.exists()) try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    private static class UpdateApkManagerSingleton {
        private static final UpdateApkManager INSTANCE = new UpdateApkManager();

    }

    public static UpdateApkManager getInstance() {
        return UpdateApkManager.UpdateApkManagerSingleton.INSTANCE;
    }

    public void getUpdateApkData() {

/*
        RetrofitJsonManagerTest.getInstance().apiService.getUpdateApkInfo(2, "42").compose(RxJavaHttpManager.<UpdateApkInfo>applyTransformer()).subscribe(new CommonObserver<UpdateApkInfo>() {
            @Override
            public void onNext(UpdateApkInfo o) {
                super.onNext(o);
                showUpdateDialogFragment(o);

            }


        });
*/

        UpdateApkInfo info = new UpdateApkInfo();
        info.url = "http://img.cooscene.com//app/beta/v1.0/beta1cooapp_version2.0_beta1.apk";
        info.version = "1.1";
        info.fileName = getFileName(info.version);
        showUpdateDialogFragment(info);
    }

    private void showUpdateDialogFragment(UpdateApkInfo info) {
        FragmentActivity fragmentActivity = AppActivityManager.getAppActivityManager().currentActivity();
        if (fragmentActivity == null) {
            return;
        }
        File apkFile = getApkFile(info.version);
        if (apkFile.exists()) {
            installAPK(apkFile);
            return;
        }

       // UpdateApkDialogFragment updateDialogFragment =  getUpdateDialogFragment();
        //updateDialogFragment.show(fragmentActivity.getSupportFragmentManager());
/*
        updateDialogFragment.setUpdateApkData(info, new DownLoadListener() {
            @Override
            public void onSuccess(Uri downIdUri, File file) {
                installAPK(file);
            }

            @Override
            public void onFailed() {

            }
        });
*/
    }

    public void installAPK(File file) {
        if (!file.exists()) return;
        // ToastUtils.showShort("正在打开文明社区" + config.version + "!");

        Intent intent = new Intent(Intent.ACTION_VIEW);
        FileUtils.getFileIntent(intent, file);
        if (AppActivityManager.getAppActivityManager().currentActivity() != null)
            AppActivityManager.getAppActivityManager().currentActivity().startActivity(intent);

    }

    boolean isCheckUpdateing = false;

    public void notifyCheckApkUpdate() {
        if (isCheckUpdateing) return;
        isCheckUpdateing = true;

    }

    public UpdateVersionDialogFragment showUpdaterDialog(final FragmentActivity activity, final UpdateApkBean updateApkBean) {
        UpdateVersionDialogFragment fragment =  getUpdateVersionDialogFragment();
        fragment.initDialogConfig(updateApkBean);
        fragment.show(activity.getSupportFragmentManager());
        return fragment;
     }

    private UpdateVersionDialogFragment getUpdateVersionDialogFragment() {

        UpdateVersionDialogFragment fragment = new UpdateVersionDialogFragment();

        return fragment;
    }

}
